import toolMgr from '@/biz/base/tool.js'
import productOpt from '@/biz/option/product.js'
import ruleOpt from '@/biz/option/rule.js'
import levelMgr from '@/biz/bonus/level.js'
import dt from '@dt/dt'

const repeatValidator = async (rule, value, callback) => {
  if (await levelMgr.repeat(value)) {
    callback()
  } else {
    callback(new Error('同项目模板名称不能重复'))
  }
}

function accessory(_this) {
  if (_this.$route.name == 'levelPointsEdit') {
    let api = toolMgr.unloadFile()
    let files = []
    if (_this.info.accessoryBOList && _this.info.accessoryBOList.length > 0) {
      files = _this.info.accessoryBOList.map((item) => ({
        url: item.accessoryUrl,
        name: item.accessoryName
      }))
    }
    return [
      {
        title: '附件上传',
        key: 'accessoryBOList',
        upload: {
          title: '上传文件',
          api,
          files,
          multiple: true,
          preview: (file) => {
            toolMgr.downloadFile(file.url, file.name)
          }
        },
        data: {
          to: (v) => {
            let accessoryBOList = v.map((item) => ({
              accessoryName: item.name,
              accessoryUrl: item.response ? item.response.data.imageURL : item.url
            }))
            return {
              accessoryBOList
            }
          }
        }
      }
    ]
  } else {
    let files = []
    if (_this.info.accessoryBOList) {
      files = _this.info.accessoryBOList.map((item) => ({
        value: item.accessoryName,
        key: item.accessoryUrl
      }))
    }
    return [
      {
        title: '附件',
        tag: {
          type: 'text',
          options: files,
          click: (tag) => {
            toolMgr.downloadFile(tag.key, tag.value)
          }
        }
      }
    ]
  }
}

function productInfo(_this) {
  return [
    productOpt.create({
      key: 'productIds',
      type: 'array',
      multiple: true,
      disabled: _this.$route.name !== 'levelPointsEdit' ? true : false,
      hid: _this.$route.query.housesId || dt.buildInfo().housesId
    })
  ]
}

function effectiveDate(_this) {
  return [
    {
      title: '生效日期',
      placeholder: '请选择生效日期',
      key: 'effectiveStartDate',
      disabled: _this.$route.name !== 'levelPointsEdit' ? true : false,
      date: {
        type: 'daterange'
      },
      data: {
        from: (info) => {
          if (info.effectiveStartDate && info.effectiveEndDate) {
            return [info.effectiveStartDate, info.effectiveEndDate]
          }
          return null
        },
        to: (v) => {
          return {
            effectiveStartDate: v[0],
            effectiveEndDate: v[1]
          }
        }
      },
      rules: [ruleOpt.required('请选择生效日期')]
    }
  ]
}

function options1(_this) {
  return [
    [
      {
        title: '模板名称',
        placeholder: '请输入模板名称',
        key: 'rankPointsName',
        disabled: _this.$route.name !== 'levelPointsEdit' ? true : false,
        input: {},
        rules: [
          ruleOpt.required('请输入模板名称'),
          ruleOpt.max(50),
          _this.$route.name == 'levelPointsEdit' ? { validator: repeatValidator, trigger: 'blur' } : {}
        ]
      }
    ],
    productInfo(_this),
    [
      {
        title: '生效方式',
        key: 'effectiveWay',
        radio: {
          options: dt.dictInfo().effectiveWay.map((i) => ({
            ...i,
            disabled: _this.$route.name !== 'levelPointsEdit' ? true : false
          }))
        },
        rules: [ruleOpt.required('请选择生效方式')]
      }
    ],
    effectiveDate(_this)
  ]
}

function options2(_this) {
  return [
    {
      title: 'oa文电编号',
      key: 'oaCode',
      placeholder: _this.$route.name == 'levelPointsEdit' ? '请输入oa文电编号' : '',
      disabled: _this.$route.name !== 'levelPointsEdit' ? true : false,
      tip: _this.$route.name == 'levelPointsEdit' ? '（若有提交oa文电，请输入oa文电编号）' : '',
      input: {},
      rules: [ruleOpt.max(20)]
    },
    accessory(_this),
    [
      {
        title: '备注',
        key: 'remark',
        disabled: _this.$route.name !== 'levelPointsEdit' ? true : false,
        input: {
          type: 'textarea',
          autosize: {
            minRows: 4
          }
        },
        rules: [ruleOpt.max(300)]
      }
    ]
  ]
}

function helps() {
  return [
    {
      label: '产品、生效方式、生效日期',
      value: ['房源根据产品、生效方式、生效日期来确定匹配的职级点数模板']
    },
    {
      label: '职级跳点',
      value: [
        '若顾问会根据指标情况进行奖金跳点，请设置“职级跳点”',
        '职级跳点支持岗位根据某指标达成情况或排名进行跳点设置，共支持42种情况设置。可针对月度或季度、个人或案场进行统计，包含签约金额、认购金额、签约面积、认购面积、签约套数、认购套数等6个指标项，根据业绩、业绩指标达成率或排名进行跳点设置'
      ]
    },
    {
      label: '可配置参考情况',
      value: [
        '顾问根据月度个人业绩，进行跳点奖金发放',
        '顾问根据月度个人业绩达成率，进行跳点奖金发放',
        '顾问根据月度个人业绩排名，进行跳点奖金发放',
        '顾问根据月度案场业绩达成率，进行跳点奖金发放',
        '主委根据季度案场业绩达成率，进行跳点奖金发放'
      ]
    },
    {
      tips: '职级点数模板在审批通过后，才可生效'
    }
  ]
}

export default {
  options1,
  options2,
  helps
}
